package com.lemon.smartsafetynew.ui.activity.ledger;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafetynew.entity.event.common.DateEvent;
import com.lemon.smartsafetynew.ui.fragment.ledger.LedgerPatrolPersonnelStatisticsFragment;
import com.lemon.smartsafetynew.ui.fragment.ledger.LedgerPatrolRecordFragment;
import com.lemon.smartsafetynew.util.constants.TimeFormatConstants;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import butterknife.Bind;

/**
 * Created by Andrew on 2017/9/14.
 */

public class LedgerPatrolStatisticsActivity extends SwipeBackActivity {

    @Bind(R.id.tabLayout)
    TabLayout tabLayout;
    @Bind(R.id.viewPager)
    ViewPager viewPager;

    @Override
    protected void initTitle() {
        this.setBarTitle("安全巡查");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(LedgerPatrolStatisticsActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_viewpage_tab_layout;
    }

    @Override
    protected void initViews() {
        super.initViews();

        // ======= 该变指示栏的长度 =======
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(tabLayout, 10, 10); //左右各缩进5dp
            }
        });

        // ======= 该变指示栏的长度 =======
        viewPager.setAdapter(new PatrolStatisticsViewpagerAdapter(getSupportFragmentManager()));
        tabLayout.setupWithViewPager(viewPager);
    }

    private class PatrolStatisticsViewpagerAdapter extends FragmentStatePagerAdapter {

        public PatrolStatisticsViewpagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    return LedgerPatrolRecordFragment.newInstance();
                case 1:
                    return LedgerPatrolPersonnelStatisticsFragment.newInstance();
                default:
                    return LedgerPatrolRecordFragment.newInstance();
            }

        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "巡查记录";
                case 1:
                    return "人员统计";
            }
            return null;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        setIconEnable(menu, true);
        getMenuInflater().inflate(R.menu.menu_calendar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_calendar: {
                pickCheckDate();
                break;
            }
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 修改日期
     */
    private void pickCheckDate() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

                Calendar calendar = Calendar.getInstance();
                calendar.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat dateFormat= new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                String dateStr = dateFormat.format(calendar.getTime());

                AppService.getBus().post(new DateEvent(dateStr, LedgerPatrolStatisticsActivity.class.getName()));

            }
        }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        dpd.setThemeDark(false);
        dpd.vibrate(false);
        dpd.show(getFragmentManager(), Constants.DIALOG_TAG_CHECK_DATE);
    }

}
